//
//  DetailController.m
//  HappyBaby
//
//  Created by lanou on 15/11/25.
//  Copyright © 2015年 华哥. All rights reserved.
//

#import "DetailController.h"
#import "HappyDetailView.h"
#import "Define.h"
#import "HappyDetailModel.h"
#import "GuessLikeModel.h"
#import "UIImageView+WebCache.h"
@interface DetailController ()<UIWebViewDelegate, DetailViewDelegate>

@property (nonatomic, strong) HappyDetailView *detailView;
@property (nonatomic, strong) HappyDetailModel *detailModel;
@property (nonatomic, strong) GuessLikeModel *guessLikeModel;
@property (nonatomic, strong) NSMutableArray *guessLikeModelArray;


@end


@implementation DetailController

- (NSMutableArray *)guessLikeModelArray
{
    if (_guessLikeModelArray == nil) {
        self.guessLikeModelArray = [NSMutableArray array];
    }
    return _guessLikeModelArray;
}

- (void)loadView
{
    self.navigationController.navigationBar.translucent = NO;
    
    self.detailView = [[HappyDetailView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64 - 49)];
    self.detailView.webView.delegate = self;
    // 给detailView设置代理
    self.detailView.delegater = self;
    self.view = self.detailView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 请求链接：http://api.bbwansha.com:8080/v22/index.php/Home/bbwspost/detail?userid=0&bbwspostid=9454
    // 创建请求链接
    NSURL *url = nil;
    if (self.happyModel) {
        url = [NSURL URLWithString:[NSString stringWithFormat:@"http://api.bbwansha.com:8080/v22/index.php/Home/bbwspost/detail?userid=0&bbwspostid=%@",self.happyModel.post_id]];
        // 给标题赋值
        self.navigationItem.title = self.happyModel.post_title;
    }else if (self.guessLikeModel) {
        url = [NSURL URLWithString:[NSString stringWithFormat: @"http://api.bbwansha.com:8080/v22/index.php/Home/bbwspost/detail?userid=0&bbwspostid=%@",self.guessLikeModel.post_id]];
        // 给标题赋值
        self.navigationItem.title = self.guessLikeModel.post_title;
    }else if (self.checkMoreModel){
        url = [NSURL URLWithString:[NSString stringWithFormat: @"http://api.bbwansha.com:8080/v22/index.php/Home/bbwspost/detail?userid=0&bbwspostid=%@",self.checkMoreModel.post_id]];
        // 给标题赋值
        self.navigationItem.title = self.checkMoreModel.post_title;
    }else if (self.carouselModel){
        url = [NSURL URLWithString:[NSString stringWithFormat:@"http://api.bbwansha.com:8080/v22/index.php/Home/bbwspost/detail?userid=0&bbwspostid=%@",self.carouselModel.banner_post_id]];
        // 给标题赋值
        self.navigationItem.title = self.carouselModel.banner_name;
    }
    // 请求数据
    [[[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        // 获取请求数据字典
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *dataDic = [dic objectForKey:@"data"];
        self.detailModel = [[HappyDetailModel alloc] init];
        // 给detailModel赋值
        [self.detailModel setValuesForKeysWithDictionary:dataDic];
        // webView加载
        [self.detailView.webView loadHTMLString:self.detailModel.post_content baseURL:nil];
        
        // 获取 post_recommends对应的数据模型
        NSArray *recommendArray = self.detailModel.post_recommends;
        for (NSDictionary *modelDic in recommendArray) {
            GuessLikeModel *guessLikeModel = [[GuessLikeModel alloc] init];
            // 为guessLikeModel赋值
            [guessLikeModel setValuesForKeysWithDictionary:modelDic];
            [self.guessLikeModelArray addObject:guessLikeModel];
        }
        dispatch_sync(dispatch_get_main_queue(), ^{
            [self.detailView.imageView sd_setImageWithURL:[NSURL URLWithString:self.detailModel.post_cover_image] placeholderImage:[UIImage imageNamed:@"placeHoldImage4"]];
            self.detailView.titleLable.text = self.detailModel.post_title;
            // 将求下来的猜你喜欢的数据赋值给detailView的数组属性
            self.detailView.guessLikeArray = self.guessLikeModelArray;
            [self.detailView.collectionView reloadData];
        });
        
    }]resume];
    
}

#pragma mark --当webView的一加载完成，就调用这个方法
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    // 给webView赋值
    CGFloat sizeHeight= [[self.detailView.webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] floatValue];
    self.hidesBottomBarWhenPushed = YES;
    [self.detailView changeDetailViewFrameWithwebViewHeight:sizeHeight];
}

#pragma mark --执行代理的方法
- (void) pushtoViewControllerWithModel:(GuessLikeModel *)guessLikeModel
{
    DetailController *detailVc = [[DetailController alloc] init];
    detailVc.guessLikeModel = guessLikeModel;
    [self.navigationController pushViewController:detailVc animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
